<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<!-- Translate returns an array of all six frames for a translation from
	 DNA/RNA to protiens -->
<script src="translate.js"></script>
</head>
<body>
<h3>Translate any number of DNA or RNA sequences to Protein <br />
	(for multiple sequences use fasta format)
</h3>

Sequence: (type or paste DNA or RNA sequence and click submit)<br />
<form name="mainForm" method="post" >
	<textarea id="dnaRna"  rows=20 cols=100 ></textarea>
	<input type="button" value="Submit" onclick="doTranslate(dnaRna.value,'frame1')" />
</form>

<p id="proteinSequence"></p>



<script>
var proteins = new Array();
doTranslate = function(mySeq, frameName)
	{
	var frameNum = frameName.replace(/frame/,"");
	//Determine if simple or fasta sequence input
	if( simple( mySeq ) )
		{
		//store variables
		proteins[0] = new Array();
		proteins[0]["orgin"] = mySeq;
		proteins[0]["Pname"] = "Protein 1";
		proteins[0]["frames"] =  translate(mySeq);
		proteins[0]["cframe"] = frameName;
		proteins[0]["cframeNum"] = frameNum;
		}
	else
		{
		//split up by fasta reference
		var solArr = [];
		var arr = mySeq.split('>');
		
		// loop through to handle all proteins
		var arrPos = 0;
		for( var i in arr  )
			{
			var seq = arr[i];
			if( ! seq )
				continue;
			//split up to find name and remove white spaces
			var miniArr = seq.split(/\n|\r\n|\n\r/);
			var pname =	miniArr.shift();
			seq = miniArr.join("");
			seq = seq.replace( /[\s\n\r]/g,"" );
			
			//store variables
			proteins[arrPos] = new Array();
			proteins[arrPos]["orgin"] = seq;
			proteins[arrPos]["Pname"] = pname;
			proteins[arrPos]["frames"] =  translate(seq);
			proteins[arrPos]["cframe"] = frameName;
			proteins[arrPos]["cframeNum"] = frameNum;
			
			//increment actual position
			arrPos++;
			}
		}
	//'print' the results
	printFrames();
				
	}
function rewritePage()
	{
	var toprint = "";
	for( i in proteins )
		{
		toprint = toprint + '>' + proteins[i]["Pname"] + proteins[i]["cframe"]+ '<br />' +
			splitChars( proteins[i]["frames"][proteins[i]["cframeNum"]-1], 70 );
		}
	toprint = toprint + '<input type="button" value="Change Back." onclick="printFrames()" />';
	document.getElementById('proteinSequence').innerHTML = toprint;
	}
function printFrames()
	{
	var toprint = "";
	for( i in proteins )
		{
		toprint = toprint + '<h4>' + proteins[i]["Pname"] +'</h4>' +
			createProteinTranslation(proteins[i]["cframe"], i) + '<br />' +
			splitChars( proteins[i]["frames"][proteins[i]["cframeNum"]-1], 70 );
		}
	toprint = toprint + '<input type="button" value="Change to fasta format." onclick="rewritePage()" />';
	document.getElementById('proteinSequence').innerHTML = toprint;
	}
function splitChars( sequence, lengthToSplit )
	{
	var toRet = "";
	var currentL = sequence.length + lengthToSplit;
	for( var i=lengthToSplit; i<currentL; i+=lengthToSplit )
		{
		toRet = toRet + sequence.slice(i-lengthToSplit, i) + "<br />";
		}
	return toRet;
	}
	
function changeFrame(frameName, protNum)
	{
	proteins[protNum]["cframe"]=frameName;
	proteins[protNum]["cframeNum"] = frameName.replace(/frame/,"");
	printFrames();
	}
	
function createProteinTranslation(frameNum, protNum)
	{
	function valCheck( pt1, pt2 )
		{
		if( pt1==pt2 )
			return " selected";
		else
			return "";
		}
	sol = '<select id="menu' +protNum +
		'" onchange="changeFrame(this.value,' + protNum + ')">' + 
		'<option value="frame1"' + valCheck(frameNum,"frame1") +'>Frame One</option>' +
		'<option value="frame2"' + valCheck(frameNum,"frame2") +'>Frame Two</option>' +
		'<option value="frame3"' + valCheck(frameNum,"frame3") +'>Frame Three</option>' +
		'<option value="frame4"' + valCheck(frameNum,"frame4") +'>Frame Four</option>' +
		'<option value="frame5"' + valCheck(frameNum,"frame5") +'>Frame Five</option>' +
		'<option value="frame6"' + valCheck(frameNum,"frame6") +'>Frame Six</option>' +
		'</select><br />'
	return sol;
	}
	
function simple(seq)
	{
	return( ! seq.match(/\>/) );
	//return true;
	}
	
	
</script>
</body>
</html>


